package org.jadestudio.home.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jadestudio.home.service.GameService;
import org.jadestudio.home.service.LeaderboardService;

public class GameListAction extends Action {
	private GameService gameService;
	private LeaderboardService leaderboardService;
	
	public GameService getGameService() {
		return gameService;
	}
	public void setGameService(GameService gameService) {
		this.gameService = gameService;
	}
	
	public LeaderboardService getLeaderboardService() {
		return leaderboardService;
	}
	public void setLeaderboardService(LeaderboardService leaderboardService) {
		this.leaderboardService = leaderboardService;
	}
	
	/**
	 * 加载数据
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		
		String category = request.getParameter("category");
		if (category==null) category="";
		String initial = request.getParameter("initial");
		if (initial==null) initial="";
		String platform = request.getParameter("platform");
		if (platform==null) platform="";
		String ps = request.getParameter("p");
		int p = 1;
		if (ps!=null && !ps.equals("")) p = Integer.parseInt(ps);		
		
		// 加载游戏类型列表
		List<java.util.Map> gameTypeList = gameService.getGameTypeList();
		request.setAttribute("gameTypeList", gameTypeList);
		
		// 加载游戏首字母列表
		List<java.util.Map> gameInitialList = gameService.getGameInitialList();
		request.setAttribute("gameInitialList", gameInitialList);
		
		// 加载游戏平台列表
		List<java.util.Map> gamePlatformList = gameService.getGamePlatformList();
		request.setAttribute("gamePlatformList", gamePlatformList);
		
		// 加载筛选游戏列表
		List<java.util.Map> gameList = gameService.getGameList(category, initial, platform, p, 20);
		request.setAttribute("gameList", gameList);
		
		// 加载筛选页总数
		int pageCount = gameService.getGamePageCount(category, initial, platform, 20);
		request.setAttribute("pageCount", pageCount);
		
		// 分类TOP10
		List<java.util.Map> gameTop10 = leaderboardService.getGameTop10ByType(category);
		request.setAttribute("gameTop10", gameTop10);
		
		// 游戏/文章总数
		java.util.Map count = leaderboardService.getGameArticleCount();
		request.setAttribute("count", count);
		
		return mapping.findForward("success");
	}
}
